package com.hnust.cleveroom;

import com.cleveroom.core.KLWCommunicater;
import com.hnust.entity.CleveroomDevice;
import com.hnust.entity.CleveroomSensor;
import com.hnust.listener.abstracts.DeviceListener;
import com.hnust.listener.abstracts.SensorListener;
import com.hnust.listener.CleveroomConnListener;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.boot.autoconfigure.condition.ConditionalOnMissingBean;
import org.springframework.boot.context.properties.EnableConfigurationProperties;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;

import java.util.List;
import java.util.concurrent.CopyOnWriteArrayList;

/**
 * 创建人：zhangRuWang
 * 创建时间：2022/4/9
 * 描述：
 */
@Configuration
@EnableConfigurationProperties(CleveroomProperties.class)
public class  CleveroomConfig {
    @Autowired
    private DeviceListener deviceListener;
    @Autowired
    private SensorListener sensorListener;
    @Autowired
    private CleveroomConnListener cleveroomConnListener;
    @Bean
    @ConditionalOnMissingBean(name = "kLWCommunicater")
    public KLWCommunicater klwCommunicater(CleveroomProperties cleveroomProperties)
            throws IllegalAccessException,
            InstantiationException {
        KLWCommunicater klwCommunicater = new KLWCommunicater(cleveroomProperties.getHost(),
                cleveroomProperties.getPort());
        if (cleveroomProperties.getLogger() != null) {
            klwCommunicater.setLogger(
                    cleveroomProperties.getLogger().newInstance()
            );
        }
        if (cleveroomProperties.getGateKey() != null) {
            klwCommunicater.setGwkey(cleveroomProperties.getGateKey());
        }
        klwCommunicater.setConnectListener(cleveroomConnListener);
        klwCommunicater.addDeviceListener("_add_device_listener",deviceListener);
        klwCommunicater.addSensorListener("_add_sensor_listener",sensorListener);
        return klwCommunicater;
    }
}
